<?php

class Admin_StoreController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/login");
        }
        $this->_helper->layout->setLayout("admin");
    }

    public function indexAction() {
        $storeDb = new Admin_Model_Store();
        $fetchAll = $storeDb->fetchAll(null, true);
        $this->view->allStores = $fetchAll;
    }

    public function addAction() {
        $form = new Admin_Form_StoreAdd();
        $stateDb = new Admin_Model_State();
        $cityDb = new Admin_Model_City();
        $zipDb = new Admin_Model_Zipcode();
        $allState = $stateDb->getAllStateArray();
        $allCity = $cityDb->getAllCityArray(false);
        $allZip = $zipDb->getAllZipArray(false);
        $this->view->allState = json_encode($allState);
        $this->view->allCity = json_encode($allCity);
        $this->view->allZip = json_encode($allZip);
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $values['location'] = $values['zip'];
                $details = json_encode(array("details" => $values));
                $data = array(
                    'detail' => $details,
                    'zip_id' => $values['zip'],
                    'city_id' => $values['city'],
                    'state_id' => $values['state'],
                    'enabled' => true
                );
                $db = new Admin_Model_Store();
                $db->addNewStore($data);
                $this->_redirect("/admin/store");
            }
        }
    }

    public function updateAction() {
        $id = $this->getinputidAction();
        $storeDb = new Admin_Model_Store();
        $storeById = $storeDb->getStoreById($id, true);
        $form = new Admin_Form_StoreAdd();
        $form->name->setValue($storeById['store_name']);
        $form->address1->setValue($storeById['address1']);
        $form->address2->setValue($storeById['address2']);
        $form->email->setValue($storeById['email']);
        $form->contact1->setValue($storeById['contact1']);
        $form->contact2->setValue($storeById['contact2']);
        // $form->location->setValue($storeById['location']['zipcode']['id']);
        $stateDb = new Admin_Model_State();
        $cityDb = new Admin_Model_City();
        $zipDb = new Admin_Model_Zipcode();
        $allState = $stateDb->getAllStateArray();
        $allCity = $cityDb->getAllCityArray(false);
        $allZip = $zipDb->getAllZipArray(false);
        $this->view->allState = json_encode($allState);
        $this->view->allCity = json_encode($allCity);
        $this->view->allZip = json_encode($allZip);
        $this->view->selectedState = $storeById['location']['state']['id'];
        $this->view->selectedCity = $storeById['location']['city']['id'];
        $this->view->selectedZip = $storeById['location']['zipcode']['id'];
        $brandsArray = array();
        foreach ($storeById['brands'] as $brnd) {
            array_push($brandsArray, $brnd['brand_id']);
        }
        $form->brands->setValue($brandsArray);
        $form->map->setValue($storeById['map']);
        $form->id->setValue($id);
        $form->submit->setLabel("Update Store");
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $details = json_encode(array("details" => $values));
                $zip_id = $values['location'];
                $zipDb = new Admin_Model_Zipcode();
                $zipWithCityArray = $zipDb->getZipWithCityArray($zip_id);
                $data = array(
                    'detail' => $details,
                    'zip_id' => $zip_id,
                    'city_id' => $zipWithCityArray['city']['id'],
                    'state_id' => $zipWithCityArray['state']['id'],
                    'enabled' => true
                );
                $storeDb->updateStore($data, $values['id']);
                $this->_redirect("/admin/store");
            }
        }
    }

    public function changestateAction() {
        $id = $this->getinputidAction();
        $storeDb = new Admin_Model_Store();
        $storeById = $storeDb->getStoreById($id);
        $blocked = false;
        if ($storeById[0]['enabled']) {
            $blocked = false;
        } else {
            $blocked = true;
        }
        $storeDb->updateStore(array(
            'enabled' => $blocked,
                ), $storeById[0]['id']);
        $this->_redirect("/admin/store");
    }

    /**
     * Get ID.    
     *
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; // id
        return $id;
    }

}

